import echarts from 'echarts';
import moment from 'moment';

/**
 * 评价-财务分析图表
 * @param {*} x 除了市值以外的时间横坐标
 * @param {*} self 指标
 * @param {*} avg 行业均值
 * @param {*} income 营业收入
 * @param {*} profit 营业利润
 * @param {*} market 市值
 */
export function getFinancialOption(x, self, avg, income, profit, market) {
  // 营业收入
  income.values = income.values.map(item => {
    return Number(item);
  });
  // 营业收入
  profit.values = profit.values.map(item => {
    return Number(item);
  });
  // 市值
  market.values = market.values.map(item => {
    return Number(item);
  });

  let option = {
    color: ['#61a0a8', '#d48265', '#88b3f5', '#ffd200', '#c4ccd3'],
    tooltip: {
      trigger: 'axis',
    },
    legend: {
      data: ['指标', '行业均值', '营业收入', '营业利润', '市值'],
    },
    // calculable: true,
    grid: {
      y: '10%',
      height: '70%',
      bottom: 0,
    },
    xAxis: [
      // self avg income profit market
      {
        // 0 指标
        type: 'category',
        data: x,
        axisLabel: {
          interval: 0,
          rotate: 30,
          fontSize: 10,
        },
      },
      {
        // 1 市值
        type: 'category',
        data: market.x,
        show: false,
      },
    ],
    yAxis: [
      {
        type: 'value',
        axisLabel: {
          //formatter: `${value} * 100 %`
          formatter: '{value}',
        },
      },
      {
        type: 'value',
        name: '市值(元)',
        axisLabel: {
          formatter: function(value, index) {
            let v = value / 100000000;
            // console.log("市值",v)
            return v + '亿';
          },
        },
      },
    ],

    series: [
      {
        name: '指标',
        type: 'line',
        data: self.values,
      },
      {
        name: '行业均值',
        type: 'line',
        data: avg.values,
      },
      {
        name: '营业收入',
        type: 'line',
        yAxisIndex: 1,
        data: income.values,
      },
      {
        name: '营业利润',
        type: 'line',
        yAxisIndex: 1,
        data: profit.values,
      },
      {
        name: '市值',
        type: 'bar',
        yAxisIndex: 1,
        xAxisIndex: 1,
        data: market.x.map((d, i) => [d, market.values[i]]),
      },
    ],
  };
  return option;
}

/**
 * 综合评价-传统财务得分
 * @param {*} title
 * @param {*} Dates
 * @param {*} industrydata
 * @param {*} owndata
 */
export function getFinanceBarOption(Dates, industrydata, owndata) {
  const option = {
    color: [
      '#D8A874',
      '#151415',
      '#ffd200',
      '#61a0a8',
      '#d48265',
      '#91c7ae',
      '#749f83',
      '#ca8622',
      '#bda29a',
      '#6e7074',
      '#546570',
      '#c4ccd3',
    ],
    tooltip: {
      trigger: 'axis',
    },
    legend: {
      data: ['本期评分', '行业均值'],
    },
    calculable: true,
    xAxis: [
      {
        type: 'category',
        data: Dates,
        axisLabel: {
          interval: 0,
          rotate: 30,
          fontSize: 11,
        },
      },
    ],
    yAxis: [
      {
        type: 'value',
      },
    ],
    series: [
      {
        name: '本期评分',
        type: 'bar',
        barWidth: '60%',
        data: owndata,
      },
      {
        name: '行业均值',
        type: 'line',
        data: industrydata,
      },
    ],
  };
  return option;
}
/**
 * 评价-相似聚类-动态相似-树形图
 * @param {*} title
 * @param {*} data
 */
export function generateTreeChartOption(title, data) {
  const option = {
    title: {
      text: title,
      x: 'center',
      subtextStyle: {
        align: 'center',
      },
    },
    tooltip: {
      formatter(params) {
        let prompt = '';
        for (let i = 0; i < params.data.info.length; i += 1) {
          if (params.data.info[i].name === 'PB' || params.data.info[i].name === 'PE') {
            prompt +=
              `${params.data.info[i].name}:` +
              `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`;
          } else {
            prompt += `${params.data.info[i].name}:&nbsp;&nbsp;&nbsp;`;
          }
          for (let j = 0; j < params.data.info[i].value.length; j += 1) {
            prompt += ` ${params.data.info[i].value[j]}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`;
          }
          prompt += '<br/>';
        }
        if (params.data.info[0].value.length > 1) {
          prompt += "得分数据分别表示'平均值'、'最小值'、'最大值'";
        }
        return prompt;
      },
    },
    series: [
      {
        type: 'tree',
        data: [data],
        top: '1%',
        left: '10%',
        bottom: '1%',
        right: '30%',
        symbolSize: (value, params) => {
          // debugger;
          if (params.data.class === 1) return 10;
          return 7;
        },
        label: {
          normal: {
            position: 'left',
            verticalAlign: 'middle',
            align: 'right',
            fontSize: 14,
          },
        },
        leaves: {
          label: {
            normal: {
              show: true,
              position: 'right',
              verticalAlign: 'middle',
              align: 'left',
            },
          },
        },
        expandAndCollapse: true,
      },
    ],
  };
  return option;
}

/**
 * 评价-相似聚类-静态相似-太阳图
 * @param {*} title
 * @param {*} data
 * @param {*} layer
 */
export function generateSunburstChartOption(title, data, layer) {
  const levels = [
    {},
    {
      r0: '0',
      r: '12%',
      itemStyle: {
        borderWidth: 1,
      },
      label: {
        rotate: 'tangential',
        align: 'center',
      },
    },
  ];
  const deg = 48 / layer;
  for (let i = 0; i < layer; i += 1) {
    levels.push({
      r0: `${12 + deg * i}%`,
      r: `${12 + deg * (i + 1)}%`,
      label: {
        rotate: 'tangential',
        align: 'center',
      },
    });
  }
  levels.push({
    r0: '60%',
    r: '62%',
    label: {
      color: 'auto',
      shadowColor: 'auto',
      // textBorderColor:'#000',
      position: 'outside',
    },
  });
  const option = {
    tooltip: {
      formatter: params => params.data.name,
    },
    title: {
      text: title,
      x: 'center',
      subtextStyle: {
        align: 'center',
      },
    },
    series: {
      type: 'sunburst',
      highlightPolicy: 'self',
      data,
      radius: [0, '90%'],
      sort: null,
      center: ['50%', '52%'],
      levels,
    },
  };
  return option;
}

/**
 * 评价-作图工具-曲线图
 * @param {*} title
 * @param {*} name
 * @param {*} dates
 * @param {*} data
 * @param {*} values
 */
export function generateProfitChartOption(title, name, dates, data, values) {
  let formatter = null;
  if (values == '原始数据' || values == '') {
    formatter = function f(value) {
      return parseFloat(value).toFixed(1) / 100000000 + '亿';
    };
  }
  let option = {
    title: {
      text: title,
      x: 'center',
    },
    tooltip: {
      trigger: 'axis',
    },
    toolbox: {
      feature: {
        // dataZoom: {
        //     yAxisIndex: 'none'
        // },
        // restore: {},
        saveAsImage: {
          title: '',
          shadowOffsetX: '-30',
        },
      },
    },
    legend: {
      x: '70%',
      y: '5%',
    },
    xAxis: {
      type: 'category',
      data: dates,
    },
    yAxis: {
      type: 'value',
      axisLabel: {
        formatter,
      },
    },
    series: [
      {
        name,
        type: 'line',
        data,
        smooth: true,
      },
    ],
  };
  return option;
}

/**
 * 首页-企业发展评估-雷达图
 * @param {*} data
 */

export function getHomeRadarChart(data) {
  // console.log("企业发展评估(雷达图):",data)
  const option = {
    color: ['#DEB07F'],
    // tooltip : {
    //   // formatter:'{a}{b}{c}'
    //   // rich: `<p>${data.compliance.score}</p>`
    // },
    radar: {
      name: {
        fontSize: '14',
        fontWeight: 'bold',
        textStyle: {
          color: '#666',
          // backgroundColor: '#fbead6',
          // borderRadius: 3,
          // padding: [3, 5],
        },
      },
      indicator: [
        { name: '合规', max: 100 },
        { name: '市场', max: 100 },
        { name: '形象', max: 100 },
        { name: '经营', max: 100 },
      ],
      triggerEvent: true,
    },

    series: [
      {
        type: 'radar',
        // tooltip : {
        //   formatter:'this.is.demo'
        //   // rich: `<p>${data.compliance.score}</p>`
        // },
        itemStyle: {
          normal: {
            areaStyle: {
              // type: 'default',
              color: ['#DEB07F'],
            },
          },
        },
        data: [
          {
            value: [
              data.compliance.score,
              data.market.score,
              data.opinion.score,
              data.manage.score,
            ],
          },
        ],
      },
    ],
  };

  return option;
}

/**
 * 首页-双折线图
 * @param {*} data
 */

export function getHomeDoubleLineChart(data) {
  // console.log("data",data.data[0].name)
  let today = moment().format('YYYY-MM-');
  // console.log('today', today);

  const option = {
    // color:[],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow',
        // type: 'cross',
        label: {
          backgroundColor: '#4585C4',
        },
      },
      formatter:
        today +
        "{b}<br /><div style='float:left; margin:5px 5px 0 0;width:10px;height:10px;border-radius:50%;background-color:#C46045'></div>{a0}:{c0}条<br /><div style='float:left; margin:5px 5px 0 0;width:10px;height:10px;border-radius:50%;background-color:#4585C4'></div>{a1}:{c1}条",
    },
    legend: {
      data: [data.data[0].name, data.data[1].name],
    },
    // toolbox: {
    //     feature: {
    //         saveAsImage: {}
    //     }
    // },
    grid: {
      top: '15%',
      left: '3%',
      right: '6%',
      bottom: 0,
      containLabel: true,
    },
    xAxis: [
      {
        type: 'category',
        boundaryGap: false,
        splitNumber: 13,
        data: data.dates,
        axisLabel: {
          // interval: 1,
          align: 'center',
          // verticalAlign:'middle',
          margin: 20,
          rotate: 34,
          fontSize: 9,
        },
      },
    ],
    yAxis: [
      {
        type: 'value',
        splitLine: {
          show: false,
        },
      },
    ],
    series: [
      {
        name: data.data[0].name,
        type: 'line',
        stack: '总量',
        smooth: true,
        sampling: 'average',
        itemStyle: {
          normal: {
            color: '#C46045',
          },
        },
        areaStyle: {
          normal: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: '#C46045',
              },
              {
                offset: 1,
                color: '#ffe',
              },
            ]),
          },
        },
        data: data.data[0].data,
      },
      {
        name: data.data[1].name,
        type: 'line',
        smooth: true,
        stack: '总量',
        sampling: 'average',
        itemStyle: {
          normal: {
            color: '#4585C4',
          },
        },
        areaStyle: {
          normal: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: '#4585C4',
              },
              {
                offset: 1,
                color: '#ffe',
              },
            ]),
          },
        },
        data: data.data[1].data,
      },
    ],
  };
  return option;
}

/**
 * 首页-柱状折线图
 * E互动
 * @param {*} data
 */

export function getHomeLineBarChart(data) {
  // console.log('E互动表内：', data.chart_data);
  let inData = data.chart_data;
  const option = {
    color: ['#D9954C', '#8b551b', '#ff0000'],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        // 坐标轴指示器，坐标轴触发有效
        type: 'shadow',
      },
      formatter: '{b}<br />新增:{c0}条<br />已回复:{c1}条',
    },
    legend: {
      // data: inData.legends,
      data: [{ name: inData.legends[0], icon: 'pin' }, { name: inData.legends[1], icon: 'pin' }],
    },
    grid: {
      top: '12%',
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true,
    },
    xAxis: {
      type: 'category',
      data: inData.x,
      axisLabel: {
        // interval: 1,
        align: 'center',
        // verticalAlign:'middle',
        margin: 20,
        rotate: 34,
        fontSize: 9,
      },
      axisTick: {
        alignWithLabel: true,
      },
    },
    yAxis: {
      type: 'value',
      splitLine: {
        show: false,
      },
    },
    series: [
      {
        name: inData.legends[0],
        type: 'bar',
        stack: '总量',
        barWidth: '60%',
        label: {
          normal: {
            show: true,
            position: 'insideTop',
          },
        },
        data: inData.y[0],
        // [4,10,7,3,13,3,null],
        // inData.y[0]
      },
      {
        name: inData.legends[1],
        type: 'bar',
        stack: '总量',
        barWidth: '60%',
        label: {
          normal: {
            show: true,
            position: 'insideTop',
          },
        },
        data: inData.y[1],
        // [null,null,1,null,5,1,2]
      },
    ],
  };
  return option;
}

/**
 * 首页-折线图
 * 市值变动图
 * @param {*} data
 */

export function getHomeLineChart(data) {
  const dataer = [
    ['2000-06-05', 116],
    ['2000-06-06', 129],
    ['2000-06-07', 135],
    ['2000-06-08', 86],
    ['2000-06-09', 73],
    ['2000-06-10', 85],
    ['2000-06-11', 73],
    ['2000-06-12', 68],
    ['2000-06-13', 92],
    ['2000-06-14', 130],
    ['2000-06-15', 245],
    ['2000-06-16', 139],
    ['2000-06-17', 115],
    ['2000-06-18', 111],
    ['2000-06-19', 309],
    ['2000-06-20', 206],
    ['2000-06-21', 137],
    ['2000-06-22', 128],
    ['2000-06-23', 85],
    ['2000-06-24', 94],
    ['2000-06-25', 71],
    ['2000-06-26', 106],
    ['2000-06-27', 84],
    ['2000-06-28', 93],
    ['2000-06-29', 85],
    ['2000-06-30', 73],
    ['2000-07-01', 83],
    ['2000-07-02', 125],
    ['2000-07-03', 107],
    ['2000-07-04', 82],
    ['2000-07-05', 44],
    ['2000-07-06', 72],
    ['2000-07-07', 106],
    ['2000-07-08', 107],
    ['2000-07-09', 66],
    ['2000-07-10', 91],
    ['2000-07-11', 92],
    ['2000-07-12', 113],
    ['2000-07-13', 107],
    ['2000-07-14', 131],
    ['2000-07-15', 111],
    ['2000-07-16', 64],
    ['2000-07-17', 69],
    ['2000-07-18', 88],
    ['2000-07-19', 77],
    ['2000-07-20', 83],
    ['2000-07-21', 111],
    ['2000-07-22', 57],
    ['2000-07-23', 55],
    ['2000-07-24', 60],
  ];

  const dateList = dataer.map(function m(item) {
    return item[0];
  });
  const valueList = dataer.map(function m(item) {
    return item[1];
  });

  const option = {
    color: ['#D9954C', '#8b551b'],
    // Make gradient line here
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999',
        },
      },
    },
    // legend: {
    //   data:['蒸发量','降水量','平均温度']
    // },
    grid: {
      top: '15%',
      left: '3%',
      right: '3%',
      bottom: '3%',
      containLabel: true,
    },
    xAxis: [
      {
        type: 'category',
        data: [
          '1月',
          '2月',
          '3月',
          '4月',
          '5月',
          '6月',
          '7月',
          '8月',
          '9月',
          '10月',
          '11月',
          '12月',
        ],
        axisPointer: {
          type: 'shadow',
        },
      },
    ],
    yAxis: [
      {
        type: 'value',
        // name: '水量',
        min: 0,
        max: 250,
        interval: 50,
        axisLabel: {
          formatter: '{value} 万',
        },
      },
      {
        type: 'value',
        // name: '温度',
        min: 0,
        max: 25,
        interval: 5,
        axisLabel: {
          formatter: '{value} %',
        },
      },
    ],
    series: [
      {
        // name:'平均温度',
        type: 'line',
        yAxisIndex: 1,
        data: [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2],
      },
    ],
  };
  return option;
}
